cmake_minimum_required( VERSION 3.15)

project(TimerPP VERSION 0.0.1)

# ---------------------------------------------------------------------------------------------
set(publish_path  ${CMAKE_CURRENT_SOURCE_DIR}/publish/${PROJECT_NAME})

set(complier_is_86_64 "")

# ---------------------------------------------------------------------------------------------
# 64位
if (CMAKE_CL_64)
    set(complier_is_86_64 "x64")
	set(PROJECT_NAME 				${PROJECT_NAME}64)
# ---------------------------------------------------------------------------------------------
# 32
else()
    set(complier_is_86_64 "x86")
endif()
# ---------------------------------------------------------------------------------------------



 # 设置 可执行程序输出目录
 set(publish_bin_debug 			${publish_path}/${complier_is_86_64}/bin/debug)
 set(publish_bin_release 		${publish_path}/${complier_is_86_64}/bin/release)

# 设置库文件输出目录
 set(publish_lib_debug 			${publish_path}/${complier_is_86_64}/lib/debug)
 set(publish_lib_release 		${publish_path}/${complier_is_86_64}/lib/release)

 set(public_include          ${publish_path}/${complier_is_86_64}/include)



set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG 				${publish_lib_debug})    
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE 				${publish_lib_release})


set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG 				${publish_bin_debug})   
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE 				${publish_bin_release}) 



# 创建文件夹
execute_process( COMMAND ${CMAKE_COMMAND} -E make_directory ${public_include})

# 拷贝头文件到输出目录
execute_process( COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/include/ITimerPP.h  ${public_include})



# ---------------------------------------------------------------------------------------------
# windows创建库
function (lib_is_on_win)

    # debug模式下增加后缀D
    set(CMAKE_DEBUG_POSTFIX "D") 

    # 个人更习惯下面的方式
    file(GLOB_RECURSE src_files 
    ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cc )

    file(GLOB_RECURSE inc_files 
    ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h)

    # 创建库
    add_library(${PROJECT_NAME} SHARED ${src_files} ${inc_files}  )

    # 指定头文件搜索路径
    target_include_directories(${PROJECT_NAME}      PRIVATE         ${CMAKE_CURRENT_SOURCE_DIR}/include)

    # 链接库
    target_link_libraries( ${PROJECT_NAME}
    PRIVATE winmm
    )

endfunction(lib_is_on_win)










# ---------------------------------------------------------------------------------------------
# windows创建库
function (lib_is_on_linux)

    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m64")

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")

    # debug模式下增加后缀D
    set(CMAKE_DEBUG_POSTFIX "D") 

    # 个人更习惯下面的方式
    file(GLOB_RECURSE src_files 
    ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cc )

    file(GLOB_RECURSE inc_files 
    ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h)

    # 创建库
    add_library(${PROJECT_NAME} SHARED ${src_files} ${inc_files}  )

    add_compile_options(-Wall -Wconversion -Wextra -pedantic)

    # 指定头文件搜索路径
    target_include_directories(${PROJECT_NAME}      PRIVATE         ${CMAKE_CURRENT_SOURCE_DIR}/include)

    TARGET_LINK_LIBRARIES(${PROJECT_NAME} rt)
    set_target_properties(${PROJECT_NAME} PROPERTIES POSITION_INDEPENDENT_CODE TRUE)

endfunction(lib_is_on_linux)



# ---------------------------------------------------------------
# 创建项目
IF (CMAKE_SYSTEM_NAME MATCHES "Linux")
    lib_is_on_linux()
ELSEIF (CMAKE_SYSTEM_NAME MATCHES "Windows")
    lib_is_on_win()
ELSEIF (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
	
ELSE ()
	MESSAGE(STATUS "other platform: ${CMAKE_SYSTEM_NAME}")
ENDIF (CMAKE_SYSTEM_NAME MATCHES "Linux")




function(lib_exam)
    set(exam_src_files
    ${CMAKE_CURRENT_SOURCE_DIR}/example/main.cc
    )

    set(exam_inc_files
    ${CMAKE_CURRENT_SOURCE_DIR}/include/ITimerPP.h
    )

    add_executable(DemoTimer ${exam_src_files} ${exam_inc_files})

    target_link_libraries(DemoTimer 
        PRIVATE ${PROJECT_NAME}
    )

    target_include_directories(DemoTimer
    PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
    )

endfunction(lib_exam)


lib_exam()
